package com.flower8.service.impl;

import com.flower8.dao.AgentDao;
import com.flower8.model.Agent;
import com.flower8.model.AgentExport;
import com.flower8.model.ExportData;
import com.flower8.service.AgentService;
import com.flower8.utils.CreateHtmlUtil;
import com.flower8.utils.PageData;
import com.flower8.utils.UserUtil;
import com.flower8.utils.ftpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: tanglin
 * @Date: 2020/5/21 15:01
 * @Version: 1.0
 */
@Service
public class AgentServiceImpl implements AgentService {
    @Autowired
    private AgentDao agentDao;


    /**
     * 根据账号id查询
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Agent getById(long id) throws Exception {
        return agentDao.getById(id);
    }

    /**
     * 根据账号查询
     * @param account
     * @return
     * @throws Exception
     */
    @Override
    public Agent getUser(String account) throws Exception {
        return agentDao.getUser(account);
    }

    /**
     * 查询渠道商列表
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getAgentList(Map param) throws Exception {
        return agentDao.getAgentList(param);
    }

    /**
     * 查询渠道商列表总数
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public int getAgentListCount(Map param) throws Exception {
        return agentDao.getAgentListCount(param);
    }

    /**
     * 修改渠道商信息
     * @param param
     * @throws Exception
     */
    @Override
    @Transactional
    public void updateAgentInfo(Map param) throws Exception {
            agentDao.updateAgentInfo(param);
    }

    /**
     * 新增渠道商
     * @param param
     * @throws Exception
     */
    @Override
    @Transactional
    public void addAgent(Map param) throws Exception {
            agentDao.addAgent(param);
    }

    /**
     * 删除渠道商
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public void deleteAgent(long id) throws Exception {
            agentDao.deleteAgent(id);
    }

    /**
     * 查询所有渠道商
     * @return
     * @throws Exception
     */
    @Override
    public List<Agent> getAllAgent() throws Exception {
        return agentDao.getAllAgent();
    }

    /**
     * 查询渠道商最大的渠道号
     * @param code
     * @return
     * @throws Exception
     */
    @Override
    public long getMaxChannelByCode(String code) throws Exception {
        return agentDao.getMaxChannelByCode(code);
    }

    /**
     * 查询渠道号列表
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getAgentChannelList(Map param) throws Exception {
        return agentDao.getAgentChannelList(param);
    }

    /**
     * 查询渠道号列表总数
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public int getAgentChannelCount(Map param) throws Exception {
        return agentDao.getAgentChannelCount(param);
    }

    /**
     * 新增渠道号
     * @param param
     * @throws Exception
     */
    @Override
    @Transactional
    public void addAgentChannel(Map param) throws Exception {
        String code = param.get("code").toString();
        //获取渠道商的最新一个渠道号
        long maxChannel = agentDao.getMaxChannelByCode(code);

        Agent agent = agentDao.getByCode(code);
        //自增渠道号
        if(maxChannel == 0){
            param.put("channelNum", String.format("%04d", agent.getId())+String.format("%05d", 1));
        }else{
            param.put("channelNum",String.format("%09d", maxChannel + 1));
        }

        //创建推广网页
        String fileName = "bjh_" + param.get("channelNum").toString()+".html";
        int type = Integer.parseInt(param.get("modelType").toString());
        String url = CreateHtmlUtil.createHtml(param.get("channelNum").toString(),fileName, type);
        param.put("downloadUrl", url);
        agentDao.addAgentChannel(param);
    }

    /**
     * 删除渠道号
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public void delAgentChannel(long id) throws Exception {
        //删除服务器上的推广页面
        Map res = agentDao.getChannelById(id);
        if(res.get("download_url") != null && !"".equals(res.get("download_url").toString())){
            String url = res.get("download_url").toString();
            String s[] = url.split("/");
            String fileName = s[s.length-1];
            ftpUtil.delFile(fileName, 7);
        }

        agentDao.delAgentChannel(id);
    }


    /**
     * 查询统计数据
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getStaticTotal(PageData pd) throws Exception {
        return agentDao.getStaticTotal(pd);
    }

    /**
     * 查询统计数据总数
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getStaticTotalCount(PageData pd) throws Exception {
        return agentDao.getStaticTotalCount(pd);
    }

    /***
     * 导出数据
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<ExportData> exportData(Map pd) throws Exception {
        return agentDao.exportData(pd);
    }

    /**
     * 查询脱敏数据列表
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getDetailList(Map pd) throws Exception {
        List<Map> res = agentDao.getDetailList(pd);
        for(Map map : res){
            if(!"".equals(map.get("userName").toString())){
                map.put("userName", UserUtil.changeUserName(map.get("userName").toString()));
            }

            if(!"".equals(map.get("mobile").toString())){
                map.put("mobile", UserUtil.changeMobile(map.get("mobile").toString()));
            }
        }
        return res;
    }

    /**
     * 查询脱敏数据列表总数
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getDetailListCount(Map pd) throws Exception {
        return agentDao.getDetailListCount(pd);
    }

    /**
     * 导出脱敏数据
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<AgentExport> exportRegisterData(PageData pd) throws Exception {
        return agentDao.exportRegisterData(pd);
    }

    /**
     * 查询中注册数和实名数
     * @param agentId
     * @return
     * @throws Exception
     */
    @Override
    public Map getTotal(Long agentId) throws Exception {
        return agentDao.getTotal(agentId);
    }


}
